package com.bqtv.sport.ui.home.fragment

import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.R
import com.bqtv.sport.adapter.FootballLineupAdapter
import com.bqtv.sport.adapter.FootballLineupBenchAdapter
import com.bqtv.sport.adapter.FootballStatisticeAdapter
import com.bqtv.sport.adapter.LiveFootballLogoAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.bean.FootballLogoBean
import com.bqtv.sport.commen.utils.DpUtil
import com.bqtv.sport.commen.widget.GridItemDecoration
import com.bqtv.sport.databinding.FragmentLiveFootballLineupBinding
import java.util.Collections


class LiveFootballLineupFragment :
    AppFragment<FragmentLiveFootballLineupBinding, AppActivity<ViewBinding>>() {

        private lateinit var homeBenchAdapte: FootballLineupBenchAdapter
        private lateinit var awayBenchAdapte: FootballLineupBenchAdapter
        private lateinit var homeStatisticeAdapter: FootballStatisticeAdapter
        private lateinit var awayStatisticeAdapter: FootballStatisticeAdapter
        private lateinit var logoAdapter: LiveFootballLogoAdapter
    companion object {
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): LiveFootballLineupFragment {
            val fragment = LiveFootballLineupFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): LiveFootballLineupFragment {
            val fragment = LiveFootballLineupFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentLiveFootballLineupBinding.inflate(layoutInflater)

    override fun initView() {
        binding.rcvBenchHome.layoutManager= LinearLayoutManager(requireContext())
        binding.rcvBenchAway.layoutManager= LinearLayoutManager(requireContext())
        homeBenchAdapte= FootballLineupBenchAdapter()
        binding.rcvBenchHome.adapter=homeBenchAdapte
        awayBenchAdapte= FootballLineupBenchAdapter()
        awayBenchAdapte.home=2
        binding.rcvBenchAway.adapter=awayBenchAdapte

        binding.rcvStatisticsHome.layoutManager= LinearLayoutManager(requireContext())
        binding.rcvStatisticsAway.layoutManager= LinearLayoutManager(requireContext())
        homeStatisticeAdapter= FootballStatisticeAdapter()
        binding.rcvStatisticsHome.adapter= homeStatisticeAdapter
        awayStatisticeAdapter= FootballStatisticeAdapter()
        binding.rcvStatisticsAway.adapter= awayStatisticeAdapter

        binding.rcvLogo.layoutManager = GridLayoutManager(requireContext(), 5)
        binding.rcvLogo.addItemDecoration(
            GridItemDecoration(
                getContext(), 5,
                GridItemDecoration.LineDrawMode.HORIZONTAL, 1, 10
            )
        )
        logoAdapter = LiveFootballLogoAdapter()
        binding.rcvLogo.adapter = logoAdapter
    }

    override fun initData() {
        var lineupHomeBean: MutableList<MutableList<String>> = ArrayList()
        var lineupAwayBean: MutableList<MutableList<String>> = ArrayList()
        lineupHomeBean.add(arrayListOf("1"))
        lineupHomeBean.add(arrayListOf("1", "2", "3", "4"))
        lineupHomeBean.add(arrayListOf("1", "2", "3", "4"))
        lineupHomeBean.add(arrayListOf("1", "2"))
        lineupAwayBean.add(arrayListOf("1"))
        lineupAwayBean.add(arrayListOf("1", "2", "3", "4"))
        lineupAwayBean.add(arrayListOf("1", "2", "3"))
        lineupAwayBean.add(arrayListOf("1", "2", "3"))
        for (i in 0..<lineupHomeBean.size) {
            val list: MutableList<String> =
                lineupHomeBean.get(i)
            val rv_player = RecyclerView(requireContext())
            rv_player.setNestedScrollingEnabled(false)
            if (lineupHomeBean.size === 4) {
                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 0, 0, DpUtil.dp2px(20))
                rv_player.setLayoutParams(layoutParams)
                binding.llRed.addView(rv_player)
            } else {
                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 0, 0, DpUtil.dp2px(8))
                rv_player.setLayoutParams(layoutParams)
                binding.llRed.addView(rv_player)
            }
            val lineupAdapter: FootballLineupAdapter = FootballLineupAdapter()
            lineupAdapter.submitList(list)
            rv_player.setLayoutManager(GridLayoutManager(getContext(), list.size))
            rv_player.setAdapter(lineupAdapter)
        }
        
        Collections.reverse(lineupAwayBean)
        for (i in 0..<lineupAwayBean.size) {
            val list: MutableList<String> =
                lineupAwayBean.get(i)
            val rv_player = RecyclerView(requireContext())
            rv_player.setNestedScrollingEnabled(false)
            if (lineupAwayBean.size === 4) {
                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 0, 0, DpUtil.dp2px(20))
                rv_player.setLayoutParams(layoutParams)
                binding.llBlue.addView(rv_player)
            } else {
                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(0, 0, 0, DpUtil.dp2px(8))
                rv_player.setLayoutParams(layoutParams)
                binding.llBlue.addView(rv_player)
            }
            val lineupAdapter: FootballLineupAdapter = FootballLineupAdapter()
            lineupAdapter.submitList(list)
            rv_player.setLayoutManager(GridLayoutManager(getContext(), list.size))
            rv_player.setAdapter(lineupAdapter)
        }

        var testList: MutableList<String> = arrayListOf("1", "2", "3", "4", "5")
        homeBenchAdapte.submitList(testList)
        awayBenchAdapte.submitList(testList)

        var logos: MutableList<FootballLogoBean> = ArrayList()
        logos.add(FootballLogoBean(R.mipmap.ic_goal, getString(R.string.goal)))
        logos.add(FootballLogoBean(R.mipmap.ic_on_target, getString(R.string.on_target)))
        logos.add(FootballLogoBean(R.mipmap.ic_shot_miss, getString(R.string.shot_miss)))
        logos.add(FootballLogoBean(R.mipmap.ic_penalty, getString(R.string.penalty)))
        logos.add(FootballLogoBean(R.mipmap.ic_missed_penalty, getString(R.string.missed_penalty)))
        logos.add(FootballLogoBean(R.mipmap.ic_own_goal, getString(R.string.own_goal)))
        logos.add(FootballLogoBean(R.mipmap.ic_assist, getString(R.string.assist)))
        logos.add(FootballLogoBean(R.mipmap.corner, getString(R.string.corner)))
        logos.add(FootballLogoBean(R.mipmap.yellow_card, getString(R.string.yellow_card)))
        logos.add(FootballLogoBean(R.mipmap.red_card, getString(R.string.red_card)))
        logos.add(FootballLogoBean(R.mipmap.ic_substitute, getString(R.string.substitute)))
        logos.add(FootballLogoBean(R.mipmap.ic_two_yellows, getString(R.string.two_yellows)))
        logos.add(FootballLogoBean(R.mipmap.ic_var, getString(R.string.varr)))
        logoAdapter.submitList(logos)
    }
}

